#!/bin/bash

usage() {
    echo "usage:"
    echo "a: arch define"
}

set -x
# 目标Android版本
API=29
# NDK的路径，根据自己的NDK位置进行设置
NDK=/code/tools/sdk/ndk/21.0.6113669
# 编译工具链路径
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
# 编译环境
SYSROOT=$TOOLCHAIN/sysroot

while getopts "m:a:x:cpvr" opt
do
    case $opt in
        a)
            arch_type="${OPTARG}"
            ;;
        ?)
            echo "there is unrecognized parameter ${opt}."
            exit 1
            ;;
    esac
done

if [[ ${arch_type} == "arm" ]]; then
  arch=arm
  cpu=armv7-a
  cross_prefix_path=$TOOLCHAIN/bin/arm-linux-androideabi-
  cross_prefix_clang_path=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-
elif [[ ${arch_type} == "arm64" ]]; then
  arch=aarch64
  cpu=armv8-a
  cross_prefix_path=$TOOLCHAIN/bin/aarch64-linux-android-
  cross_prefix_clang_path=$TOOLCHAIN/bin/aarch64-linux-android$API-
elif [[ ${arch_type} == "x86" ]] || [[ ${arch_type} == "x86_64" ]]; then
  arch=x86_64
  cpu=x86_64
  cross_prefix_path=$TOOLCHAIN/bin/x86_64-linux-android-
  cross_prefix_clang_path=$TOOLCHAIN/bin/x86_64-linux-android$API-
fi

#so库输出目录
OUTPUT=/code/freemeos-code/anbox-cmcc/ff/out-${arch}

function build {
  ./configure \
  --prefix=$OUTPUT \
  --target-os=android \
  --arch=${arch} \
  --cpu=${cpu} \
  --enable-asm \
  --enable-neon \
  --enable-cross-compile \
  --enable-shared \
  --disable-static \
  --disable-doc \
  --disable-ffplay \
  --disable-ffprobe \
  --disable-symver \
  --disable-ffmpeg \
  --sysroot=$SYSROOT \
  --cross-prefix=${cross_prefix_path} \
  --cross-prefix-clang=${cross_prefix_clang_path} \
  --extra-cflags="-fPIC"

  make clean all
  # 这里是定义用几个CPU编译
  make -j12
  make install
}

build
